﻿using System;
using System.Configuration;
using System.Linq;

namespace OpenNOSClient
{
    class Program
    {
        private static IOpenNOSRestClient client;

        static void Main(string[] args)
        {
            string key = ConfigurationManager.AppSettings["OpenNOSKey"];
            client = OpenNOSRestClient.Create(key);

            TestSearch("brand AND asbest");
            TestSearch("water");

            TestLatestArticles(OpenNOSCategory.Nieuws);
            TestLatestArticles(OpenNOSCategory.Sport);

            TestLatestAudioAndVideo(OpenNOSCategory.Nieuws);
            TestLatestAudioAndVideo(OpenNOSCategory.Sport);

            TestGuide(DateTime.Now, DateTime.Now.AddDays(1));
        }

        private static void TestLatestAudioAndVideo(OpenNOSCategory category)
        {
            Console.WriteLine("Getting latest audio and video for : '{0}'", category);
            var start = DateTime.Now;
            var audio = client.GetLatestAudio(category).Select(a => (MultimediaItem)a);
            var video = client.GetLatestVideo(category).Select(v => (MultimediaItem)v);

            foreach (MultimediaItem item in audio.Union(video))
            {
                Console.WriteLine(item.Title);
            }
            var end = DateTime.Now;
            Console.WriteLine((end - start).Milliseconds);
            Console.WriteLine();
        }

        private static void TestLatestArticles(OpenNOSCategory category)
        {
            Console.WriteLine("Getting latest articles for : '{0}'", category);
            var start = DateTime.Now;
            var articles = client.GetLatestArticles(category);

            foreach (var article in articles)
            {
                Console.WriteLine(article.Title);
            }
            var end = DateTime.Now;
            Console.WriteLine((end - start).Milliseconds);
            Console.WriteLine();
        }

        private static void TestSearch(string query)
        {
            Console.WriteLine("Searching for : '{0}'", query);
            var start = DateTime.Now;
            var result = client.SearchContent(query);
            foreach (var doc in result.Documents)
            {
                Console.WriteLine(doc.Title);
            }
            var end = DateTime.Now;
            Console.WriteLine((end - start).Milliseconds);
            Console.WriteLine();
        }

        private static void TestGuide(DateTime startDate, DateTime endDate)
        {
            Console.WriteLine("Get Audio and TV Guide from '{0}' to {1}", startDate, endDate);
            var start = DateTime.Now;
            var resultRadio = client.GetRadioGuide(startDate, endDate, null);
            var resultTV = client.GetTVGuide(startDate, endDate, null);
            foreach (var doc in resultRadio.Union(resultTV))
            {
                Console.WriteLine(doc.Date);
                foreach (var guideItem in doc.GuideItems)
                {
                    Console.WriteLine("(" + guideItem.StartTime + ", " + guideItem.EndTime + ") " + guideItem.ChannelCode + ", " + guideItem.Title);
                }
            }
            var end = DateTime.Now;
            Console.WriteLine((end - start).Milliseconds);
            Console.WriteLine();
        }
    }
}
